﻿
layui.define( function (exports) {
    var $ = layui.jquery;
    ctrl = {};
    ctrl.login = function(){
        winui.lockScreen(function (user_name,password) {
            //模拟解锁验证
            if (password === 'admin' && user_name === "admin") {
                window.localStorage.login_success = true;
                return true;
            } else {
                winui.window.msg('密码错误', { shift: 6 });
                return false;
            }
        });
    };

    // 判断是否显示锁屏（这个要放在最后执行）
    if (window.localStorage.getItem("login_success") !== "true") {
        ctrl.login
    }

//注销登录
    $('.logout').on('click', function () {
        winui.hideStartMenu();
        winui.window.confirm('确认注销吗?', { icon: 3, title: '提示' }, function (index) {
            ctrl.login
        });
    });
    ctrl.url_ = function(e){
        return location.origin+"/"+e;
    }
    var win_index = 0;
    window.open_window = function(url,title){
        var content = '';
        $.ajax({
            type: 'get',
            url: url,
            async: false,
            success: function (data) {
                content = data;
            },
            error: function (e) {
                var page = '';
                switch (e.status) {
                    case 404:
                        page = '404.html';
                        break;
                    case 500:
                        page = '500.html';
                        break;
                    default:
                        content = "打开窗口失败";
                }
                $.ajax({
                    type: 'get',
                    url: 'views/error/' + page,
                    async: false,
                    success: function (data) {
                        content = data;
                    },
                    error: function () {
                        layer.close(load);
                    }
                });
            }
        });
    //核心方法（参数请看文档，config是全局配置 open是本次窗口配置 open优先级大于config）
    winui.window.config({
        anim: 0,
        miniAnim: 0,
        maxOpen: -1
    }).open({
        id: win_index,
        type: 1,
        title: "Ctrl",
        content: content
        , maxOpen: -1
    });
    };

    window.open_register_window = function(){
        open_window(url_("/index/index/register"),"注册");
    };

    //发布指令
    ctrl.send=function(){
        var url = ctrl.url_("index/test/index")+"?device_id=1&msg=730 can you resp";
        $.get(url,function(){
            winui.window.msg('发送成功', { shift: 6 });
        });
    };
    window.ctrl = ctrl;
    exports('ctrl', { });
});